package cn.neusoft.controller;

import cn.neusoft.domain.ReturnedInfo;
import cn.neusoft.domain.Supplier;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.query.AllocationQuery;
import cn.neusoft.query.SupplierQuery;
import cn.neusoft.service.SupplierService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/supplier")
@Api(value = "供应商模块的API",description="供应商相关的CRUD功能")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    @Autowired
    public SupplierController(SupplierService supplierService) {
        this.supplierService = supplierService;
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加供应商" )
    public void addSupplier(@RequestBody Supplier supplier) {
        supplier.setSupplier_id(String.valueOf(UUID.randomUUID()));
        supplierService.add(supplier);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除供应商" )
    public void delete(@PathVariable String id) {
        supplierService.delete(id);
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新供应商信息" )
    public void updateSupplier(@RequestBody Supplier supplier) {
        supplierService.update(supplier);
    }

    @GetMapping("/all")
    @ApiOperation(value = "查看所有供应商信息" )
    public List<Supplier> getAllSuppliers() {
        return supplierService.getAll();
    }

    @GetMapping("/getByName/{name}")
    @ApiOperation(value = "根据名字查询供应商信息" )
    public List<Supplier> getSupplierByName(@PathVariable String name) {
        return supplierService.getByName(name);
    }

    @GetMapping("/page/{page}/{pageSize}")
    public List<Supplier> getSuppliersByPage(@PathVariable int page, @PathVariable int pageSize) {
        return supplierService.getSuppliersByPage(page, pageSize);
    }

    @GetMapping("/totalCount")
    public int getTotalCount() {
        return supplierService.getTotalCount();
    }

    @PostMapping("/page")  //  Post + /customer/page
    @ApiOperation(value = "分页查询供应商信息" )
    public ResultBean findByPage(@RequestBody SupplierQuery supplierQuery){
        PageBean<Supplier> pageBean = supplierService.findByPage(supplierQuery);
        return ResultBean.success(pageBean);
    }
    /**
     * @Description:退货管理，通过查询已经完成入库操作的调拨单，生成对应的中心库房出库调拨单
     * @Author:邢旭升
     * @date: 2023/7/11 上午 11:25
     **/



    @PostMapping("/getReturnedAllocate")
    public ResultBean getReturnedAllocate(@RequestBody AllocationQuery query){
        if(query.getPageSize()<0||query.getPageNow()<1){
            return ResultBean.error(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }
        PageBean<ReturnedInfo> pageBean = supplierService.getReturnToSupplierInfo(query);
        return ResultBean.success(pageBean);
    }

}
